exports.findByVehicleID = function(vehicleId) {
	var fillups = [];
	
	var Database = require('/database/Database');
	var db = new Database();
	
    var fillupsResultSet = db.execute(
    	'select v.make, v.model, vf.ID, vf.fillup_date, vf.vehicle_odometer, vf.fillup_units, ' + 
    	'vf.fillup_units*vf.fillup_price_per_unit as fill_cost ' +
    	'from vehicle_fillup vf inner join vehicle v on (vf.vehicleID=v.ID) ' + 
    	'where vf.vehicleId=? order by vf.fillup_date',
    	vehicleId
    );
    
	while(fillupsResultSet.isValidRow())
	{
		var vehicleName = fillupsResultSet.fieldByName('make') + ' ' + fillupsResultSet.fieldByName('model');
		var fillupId = fillupsResultSet.fieldByName('ID');
		var fillupDate = fillupsResultSet.fieldByName('fillup_date');
		var vehicleOdometer = fillupsResultSet.fieldByName('vehicle_odometer');
		var fillupUnits = fillupsResultSet.fieldByName('fillup_units');
		var fillCost = fillupsResultSet.fieldByName('fill_cost');
		
		// fillupDate in the database is in Unix timestamp format, so we convert it
		// to a date object
		fillupDateObj = new Date(fillupDate*1000);
		fillups.push({
			vehicleName: vehicleName,
			fillupId: fillupId,
			fillupDate: fillupDateObj,
			vehicleOdometer: vehicleOdometer,
			fillupUnits: fillupUnits,
			fillCost: fillCost
		});
		
		fillupsResultSet.next();
	}
	
	fillupsResultSet.close();
	db.close();
	
	return fillups;
}

exports.save = function(vehicleId, odometer, quantity, price, date, isPartial) {
	var realDateObject = new Date(date);
	var unixTime = Math.round((realDateObject).getTime() / 1000);
	
	var Database = require('/database/Database');
	var db = new Database();
	
	if(isPartial) {
		// First we make sure that we do not add a partial fillup as the first fillup
		var isFirstFillupRow = db.execute(
			'select count(*) from vehicle_fillup where vehicleID=?', vehicleId);
		var numberOfExistingFillups = isFirstFillupRow.field(0);
		if(numberOfExistingFillups == 0) {
			throw "Can't add a partial fill-up as the first fill-up";
		}
	}
	
	var insertFillUpQuery = db.execute('insert into vehicle_fillup ' + 
		'(ID, vehicleID, vehicle_odometer, fillup_date, fillup_units, fillup_price_per_unit, belongs_to_fillup) ' +
	 	'values (NULL, ?, ?, ?, ?, ?, ?);',
	 	vehicleId, odometer, unixTime, quantity, price,
	 	(isPartial ? 0 : null));
	 	
	// We get the fillup ID that we just inserted
	var newFillupId = db.execute('select last_insert_rowid()').field(0);
	
	if(!isPartial) {
		// Seeing that this is not a partial fillup, we want to make sure that any
		// potential partial fillups that came before, have their belongs_to_fillup column
		// pointed at this new fillup.
		db.execute(
			'update vehicle_fillup set belongs_to_fillup=? where vehicleID=? and belongs_to_fillup=0',
			newFillupId, vehicleId
		);
	}
	db.close();
	
	return newFillupId;
}

exports.removeAll = function(vehicleId) {
	var Database = require('/database/Database');
	var db = new Database();
	
	// Then delete vehicle_fillup entries
	db.execute('delete from vehicle_fillup where vehicleID=?', vehicleId);
	
	db.close();
}

exports.findByFillupID = function(fillupId) {
    var fillup = {};
    
	var Database = require('/database/Database');
	var db = new Database();
	
    var fillupResultSet = db.execute(
    	'select vehicle_odometer, fillup_date, fillup_units, fillup_price_per_unit, ' + 
    	'belongs_to_fillup from vehicle_fillup where ID=?',
    	fillupId
    );
    
	if(fillupResultSet.isValidRow())
	{
		var fillupDate = fillupResultSet.fieldByName('fillup_date');
		var vehicleOdometer = fillupResultSet.fieldByName('vehicle_odometer');
		var fillupUnits = fillupResultSet.fieldByName('fillup_units');
		var pricePerUnit = fillupResultSet.fieldByName('fillup_price_per_unit');
		var belongs_to_fillup = fillupResultSet.fieldByName('belongs_to_fillup');
		
		var isPartial = false;
		if(belongs_to_fillup != null) {
			isPartial = true;
		}
		
		// fillupDate in the database is in Unix timestamp format, so we convert it
		// to a date object
		fillupDateObj = new Date(fillupDate*1000);
		fillup = {
			fillupDate: fillupDateObj,
			vehicleOdometer: vehicleOdometer,
			fillupUnits: fillupUnits,
			pricePerUnit: pricePerUnit,
			isPartial: isPartial
		};
	}
	
	fillupResultSet.close();
	db.close();
	
	return fillup;
}


exports.update = function(fillupId, odometer, quantity, price, date) {
	if(fillupId == null) { 
		return;
	}
	
	var realDateObject = new Date(date);
	var unixTime = Math.round((realDateObject).getTime() / 1000);
	
	var Database = require('/database/Database');
	var db = new Database();
	
	var updateFillUpQuery = db.execute(
		'update vehicle_fillup set ' +
		'vehicle_odometer=?, fillup_date=?, fillup_units=?, fillup_price_per_unit=? ' +		 
		'where id=?',
		odometer, unixTime, quantity, price, fillupId
	);
	 	
	db.close();
}